package com.example.demo.service.impl;

import com.example.demo.mapper.OrderMapper;
import com.example.demo.pojo.Dto.OrderCDto;
import com.example.demo.pojo.Dto.OrderDto;
import com.example.demo.pojo.Order;
import com.example.demo.pojo.OrderItem;
import com.example.demo.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Override
    public List<Order> selectByUid(int uid) {
        return orderMapper.selectByUid(uid);
    }

    @Override
    public Map<String, Object> selectOderAll(int uid) {
        return orderMapper.selectOderAll(uid);
    }

    @Override
    public PageInfo<Order> selectOrder(int uid, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Order> list=orderMapper.selectOrder(uid);
        PageInfo<Order> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Integer> selectOrderParam(int uid) {
        return orderMapper.selectOrderParam(uid);
    }

    @Override
    public Order selectByOrderNo(String orderNo) {
        return orderMapper.selectByOrderNo(orderNo);
    }

    @Override
    public List<OrderDto> selectOrderItem(int id) {
        return orderMapper.selectOrderItem(id);
    }

    @Override
    public int cancelOrder(String orderno) {
        return orderMapper.cancelOrder(orderno);
    }

    @Override
    public OrderCDto insertOrder(int[] ids,Order order) {
        String orderno=generateOrderno();
        order.setOrderno(orderno);
        //插入到mall_order
        orderMapper.insertOrder(order);
        //根据mall_cart的id查询goodsid,price,nums
        List<OrderItem> list=orderMapper.findByIds(ids);
        System.out.println(list);
        //给mall_order_item 里面的orderid设置id
        for(OrderItem orderItem:list){
            orderItem.setOrderid(order.getId());
        }
        //插入数据到mall_cart_item
        orderMapper.insertOrderItem(list);
        //删除购物车信息
        orderMapper.deleteCart(ids);
        OrderCDto orderCDto=new OrderCDto();
        orderCDto.setId(order.getId());
        orderCDto.setOrderno(orderno);
        orderCDto.setPrice(order.getMoney());
        return orderCDto;
    }

    @Override
    public int update(String orderno) {
        return orderMapper.updateOrder(orderno);
    }

    public String generateOrderno(){
        //生成订单编号：WN + 20200903150222 + 6885(随机)
        String orderno = "LS";

        //获取当前时间
        Date now = new Date();
        //创建格式化器
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = simpleDateFormat.format(now);

        //生成随机的四位数
        Random random = new Random();
        String num = random.nextInt(10000) + "";  //0-9999  0006
        for (int i = 0; i <4 - num.length(); i++) {
            num = "0" + num;
        }
        //返回结果
        return orderno + time + num;
    }
}
